package user_local_ewallet

import (
	"context"
	"strconv"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/tool"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLocalEWalletUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 更新
func NewUserLocalEWalletUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLocalEWalletUpdateLogic {
	return &UserLocalEWalletUpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserLocalEWalletUpdateLogic) UserLocalEWalletUpdate(req *types.UserLocalEwalletUpdateRequest) (resp *types.UserLocalEwalletUpdateResponse, err error) {
	var visionLevel string
	if len(req.VisionLevel) == 0 {
		visionLevel = ""
	} else {
		visionLevel = tool.ConvertInt32SliceToString(req.VisionLevel)
	}

	_, err = l.svcCtx.AssetsRpc.UserChannel.UserLocalEWalletsUpdate(l.ctx, &rpc.UserLocalEwalletUpdateRequest{
		Id:               req.Id,
		UserId:           req.UserId,
		Type:             strconv.FormatInt(int64(req.Type), 10),
		Owner:            req.Owner,
		VisionLevel:      visionLevel,
		Phone:            req.Phone,
		Code:             req.Code,
		RevenueMin:       req.RevenueMin,
		RevenueMax:       req.RevenueMax,
		FixedDailyLimit:  req.FixedDailyLimit,
		RemainDailyLimit: req.RemainDailyLimit,
		IncomeDailyLimit: req.IncomeDailyLimit,
		DailyLimit:       req.DailyLimit,
		Sort:             req.Sort,
		Remake:           req.Remark,
		IsVision:         req.IsVisible,
	})
	if err != nil {
		return &types.UserLocalEwalletUpdateResponse{
			IsUpdate: false,
		}, nil
	}
	return &types.UserLocalEwalletUpdateResponse{
		IsUpdate: true,
	}, nil
}
